﻿namespace TiffWriteRead
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string mode = "read"; // "write" 或 "read"
            if (mode == "write")
            {
                // 写入示例
                TIFFINFO info = new TIFFINFO
                {
                    XOriginRes = 512,
                    YOriginRes = 256,
                    XReal = 2.5,
                    YReal = 2.5,
                    Angle = 99.0,
                    XyUnit = "um",
                    ZUnit = "um",
                    XShowRes = 50,
                    YShowRes = 50,
                    ROI_x1 = 0.1f,
                    ROI_y1 = 0.1f,
                    ROI_x2 = 0.2f,
                    ROI_y2 = 0.2f
                };

                // 创建测试数据
                info.OriginPoints = new List<double>(new double[512 * 256 * 10]);
                for (int i = 0; i < info.OriginPoints.Count; i++)
                {
                    info.OriginPoints[i] = (float)(Math.Sin(i * 0.01) * 100);
                }

                // 创建显示数据（50x50的RGB图像，中间画一个白色圆形）
                info.ShowPoints = new List<byte>(new byte[50 * 50 * 3]);
                int centerX = 20;
                int centerY = 20;
                int radius = 10;
                for (int y = 0; y < 50; y++)
                {
                    for (int x = 0; x < 50; x++)
                    {
                        int distanceSquared = (x - centerX) * (x - centerX) + (y - centerY) * (y - centerY);
                        if (distanceSquared <= radius * radius)
                        {
                            info.ShowPoints[(y * 50 + x) * 3 + 0] = 255; // R
                            info.ShowPoints[(y * 50 + x) * 3 + 1] = 255; // G
                            info.ShowPoints[(y * 50 + x) * 3 + 2] = 255; // B
                        }
                    }
                }
                bool success = TiffHelper.WriteDataToTiff(Path.Combine(AppContext.BaseDirectory, "test_output.tiff"), info);
                Console.WriteLine(success ? "写入成功!" : "写入失败!");
            }
            else if (mode == "read")
            {
                TIFFINFO info = new TIFFINFO();
                bool success = TiffHelper.ReadDataFromTiff(Path.Combine(AppContext.BaseDirectory, "Config", "MP1.tiff"), info);
                if (success)
                {
                    Console.WriteLine("TIFF文件读取成功!");
                    Console.WriteLine($"显示分辨率: {info.XShowRes} x {info.YShowRes}");
                    Console.WriteLine($"原始分辨率: {info.XOriginRes} x {info.YOriginRes}");
                    Console.WriteLine($"实际尺寸: {info.XReal} x {info.YReal} {info.XyUnit}");
                    Console.WriteLine($"角度: {info.Angle}");
                    Console.WriteLine($"ROI: ({info.ROI_x1}, {info.ROI_y1}) - ({info.ROI_x2}, {info.ROI_y2})");
                    Console.WriteLine($"原始数据点数: {info.OriginPoints.Count}");
                    Console.WriteLine($"显示数据字节数: {info.ShowPoints.Count}");
                }
                else
                {
                    Console.WriteLine("TIFF文件读取失败!");
                }
            }
            Console.ReadKey();
        }
    }
}
